#include "sdlSound.h"

unsigned sdlSound::counter = 0;
sdlMusicMonitor* sdlSound::monitor = NULL;

void sdlSound::init(){
	if (monitor == NULL){
		monitor = sdlMusicMonitor::getInstance();
	}
	counter++;
}

sdlSound::sdlSound(){
	this->init();
	this->sound = NULL;
}

sdlSound::sdlSound(std::string path){
	this->init();
	this->load(path);
}

bool sdlSound::load(std::string path){
	if (this->sound!=NULL){
		Mix_FreeChunk(this->sound);
		this->sound = NULL;
	}
	this->sound = Mix_LoadWAV(path.c_str());
	return this->sound!=NULL;
}

void sdlSound::play(){
	Mix_PlayChannel(-1, this->sound, 0);
}

sdlSound::~sdlSound(){
	counter--;
	Mix_FreeChunk(this->sound);
	if (counter == 0){
		sdlMusicMonitor::eliminar();
		monitor = NULL;
	}
}
